package com.zhangpeng520.login.controller;

import com.zhangpeng520.common.security.SecurityUtils;
import com.zhangpeng520.login.service.LoginService;
import com.zhangpeng520.user.entity.User;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by Administrator on 2017/8/8 0008.
 */

@Controller
@RequestMapping("login")
public class LoginController {

    @Autowired
    private LoginService loginService;

    /**
     * @Author zhangpeng(www.zhangpeng520.com)
     * @Date 2017/8/9 0009 15:59
     * @Desripition 登录跳转
     */
    @RequestMapping
    public String login() {

        return "login";
    }


    /**
     * @Author zhangpeng(www.zhangpeng520.com)
     * @Date 2017/8/9 0009 15:59
     * @Desripition 校验登录
     */
    @ResponseBody
    @RequestMapping("/check")
    public String checklogin(HttpServletRequest Request) throws Exception {
        String username = Request.getParameter("username");
        String password = Request.getParameter("password");
        User user = loginService.findLoginUser(username);
        if (user != null) {


            if (SecurityUtils.checkpassword(password,user.getPassword())) {


                Request.getSession().setAttribute("loginInfo",user);
                return "login_success";
            }
            else {
                return "login_fail";
            }
        } else {

            return "login_fail";
        }

    }

    @RequestMapping("/loginout")
    public String loginout(HttpServletRequest request)
    {

        request.getSession().removeAttribute("loginInfo");

        return "login";
    }
}
